<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>管理</a>
        <a>
          <cite>学员管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">

        <div class="site-demo-button" lay-filter="selBtn" id="typeclass">
        <button class="layui-btn site-demo-active layui-btn-warm" data-type="all">学员管理</button>
            <button class="layui-btn site-demo-active" data-type="no_all">未排课学员</button>
        <button class="layui-btn site-demo-active" data-type="no_sign_up">未报名学员</button>
        <button class="layui-btn site-demo-active" data-type="sign_up">在读学员</button>
        <button class="layui-btn site-demo-active" data-type="past_period">往期学员</button>
        <button class="layui-btn site-demo-active" data-type="renewal_charge">待缴费学员</button>

        <!--
        <a href="/admin/model/student[正式学员模板].xlsx" class="layui-btn layui-btn-radius layui-btn-normal" id="upload_student"><i class="layui-icon layui-icon-download-circle"></i>下载学员模板</a>
       -->
        <div style="float: right;margin: 20px 0 20px 0;">
            <button data-method="offset" data-type="r" class="layui-btn layui-btn-sm layui-btn-primary layui-btn-radius"
                    id="assignment">转给同事
            </button>
            <button  class="layui-btn layui-btn-sm layui-btn-primary layui-btn-radius" style="visibility:visible;"
                    id="again_up">报名
            </button>
            <button  class="layui-btn layui-btn-sm layui-btn-primary layui-btn-radius" style="visibility:hidden;"
                     id="schedule_class">排课
            </button>
        </div>
    </div>
    <div class="layui-row"
         style="padding-left:20px;margin-top:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <input type="hidden" id="campus_id">
            <div class="layui-input-inline"  style="width: 100px;">
                <select name="stype">
                    <option value="">学员姓名</option>
                    <option value="2">学员手机</option>
                    <option value="3">家长姓名</option>
                    <option value="4">家长手机</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" name="keywords" placeholder="请输入搜索关键词" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="select_campus" lay-search="">
                    <option value="">请选择校区</option>
                    {volist name="campus" id="v"}
                    <option value="{$v.id}">{$v['campus_name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="user_id" lay-search="" lay-filter="user_id">
                    <option value="">请选择课程顾问</option>
                    {volist name="users" id="user"}
                    <option value="{$user.id}">{$user['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline" style="width: 140px;">
                <select name="status_type" lay-search="">
                    <option value="">最后操作时间</option>
                    <option value="2">下次待跟进时间</option>
                    <option value="3">创建时间</option>
                    <option value="4">生日</option>
                    <option value="5">登记日期</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" style="width: auto;" name="date_range"  id="range_date" placeholder="选择日期范围" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="channel" lay-search="" lay-filter="channel">
                    <option value="">请选择来源</option>
                    {volist name="data[2]" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="grade_headmaster" lay-search="" lay-filter="channel">
                    <option value="">请选择班主任</option>
                    {volist name="headmaster" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="grade_class" lay-search="" lay-filter="channel">
                    <option value="">请选择班级</option>
                    {volist name="grades_class" id="v"}
                    <option value="{$v.id}">{$v['grades_name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="timetable" id="timetable" lay-search="" >
                    <option value="">是否排课</option>
                    <option value="1">已排</option>
                    <option value="0">未排</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="personal_center_status" lay-search="" >
                    <option value="">绑定个人中心</option>
                    <option value="1">已绑定</option>
                    <option value="0">未绑定</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="follow_up" lay-search="" lay-filter="follow_up">
                    <option value="">请选择跟进状态</option>
                    {volist name="data[6]" id="v"}
                    <option value="{$v.id}">{$v['name']}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
                <!--
                <button type="button"  class="layui-btn layui-btn-radius layui-btn-normal" id="btn_student"><i class="layui-icon layui-icon-upload"></i>导入Excel</button>
                -->
            </div>
        </div>
    </div>
    <div  id="div_student" style="display: block">
      <table class="layui-table" id="datalist_student" lay-filter="Event" ></table>
    </div>
    <div id="div" style="display: none">
      <table class="layui-table" id="datalist" lay-filter="Event"></table>
    </div>
    <script type="text/html" id="name">
        <a href="/admin/student/information/id/{{d.id}}" id="test" class="layui-table-link">{{ d.name }}</a>
    </script>

    <script type="text/html" id="barTable">
        {{# if(d.sign_up == 1 && d.arrears_status == 0){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="sign_up">补缴</a>
        {{# } else if(d.sign_up == 1  && d.arrears_status == 1 && d.conclusion_status == 0){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal layui-btn-disabled">报名</a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="sign_up">报名</a>
        {{# } }}

    </script>


    <input type="hidden"  id="contract_id">
    <input type="hidden"  id="contract_name">
    <input type="hidden" value="1" id="type_choice">
    <input type="hidden" value="all" id="type_data">
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>

    function setContract_infos(contract_id,contract_name) {
        $("#contract_id").val(contract_id);
        $("#contract_name").val(contract_name);
    }

    layui.use(['table', 'laydate','element','upload', 'layer','form'], function () {
        let table = layui.table,
            laydate = layui.laydate,
            layer = layui.layer,
            element = layui.element,
            upload = layui.upload,
            $ = layui.$;

        //限定可选日期
        laydate.render({
            elem: '#range_date'
            ,range: true
            ,trigger: 'click'
        });

        $('#addNewStudent').click(function () {
            location.href = "{:url('student/add')}";
        });

        $('#addNewContract').click(function () {
            location.href = "{:url('Student/course_contract')}";
        });
     ;

        let loading = layer.load();
        //数据表格 方法级渲染
        let tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Student/lists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'name', title: '姓名',style:'color:#01AAED;font-weight: bold;',width: 150, align: 'center', templet: '#name'},
                {field: 'phone', title: '手机号',width: 200, align: 'center'},
                {field: 'next_follow_up_record', title: '下次跟进时间', width: 250, align: 'center'},
                {field: 'personal_center_status', title: '学生个人中心',width: 300, align: 'center'},
                {field: 'sex', title: '性别', width: 100,align: 'center'},
                {field: 'channel', title: '来源',width: 100, align: 'center'},
                {field: 'course_consultant', title: '课程顾问',width: 150, align: 'center', sort: true},
                {field: 'follow_up_text', title: '跟进状态', width: 100, align: 'center'},
                {field: 'headmaster', title: '班主任', width: 150, align: 'center'},
                {field: 'timetable', title: '是否排课',width: 100, align: 'center'},
                /*
                {field: 'intentional_grade_text', title: '意向级别', align: 'center', sort: true},
                */
                {field: 'class_ratio', title: '已排课次/总课次',width: 200, align: 'center'},
                {field: 'balance', title: '学费余额', width: 200,style:'color:#00000;font-weight: bold;',  align: 'center'},
                {field: 'storedvalue', title: '账户余额', width: 200,style:'color:#00000;font-weight: bold;',  align: 'center'},
                {field: 'register_date', title: '登记日期', width: 200, align: 'center', sort: true},
                {title: '操作', fixed: 'right',width: 100, align: 'center', toolbar: '#barTable'},
            ]]
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });



        //数据表格 方法级渲染 客户管理
        let tableIns_student = table.render({
            elem: '#datalist_student'
            , url: '{:url("Student/lists")}?p='+1
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'name', title: '姓名',style:'color:#01AAED;font-weight: bold;', width: 150, align: 'center', templet: '#name'},
                {field: 'phone', title: '手机号',width: 200, align: 'center'},
                {field: 'next_follow_up_record', title: '下次跟进时间', width: 250, align: 'center'},
                {field: 'personal_center_status', title: '学生个人中心',width: 300, align: 'center'},
                {field: 'sex', title: '性别', width: 100,align: 'center'},
                {field: 'channel', title: '来源',width: 100,  align: 'center'},
                {field: 'course_consultant', title: '课程顾问',width: 150, align: 'center', sort: true},
                {field: 'follow_up_text', title: '跟进状态', width: 100, align: 'center'},
                {field: 'headmaster', title: '班主任', width: 150, align: 'center'},
                {field: 'timetable', title: '是否排课',width: 100, align: 'center'},
                {field: 'class_ratio', title: '已排课次/总课次',width: 200, align: 'center'},
                /*
                {field: 'register_date', title: '未课消学费',width: 200, align: 'center'},
                */
                {field: 'balance', title: '学费余额', width: 200,style:'color:#00000;font-weight: bold;',  align: 'center'},
                {field: 'storedvalue', title: '账户余额', width: 200,style:'color:#00000;font-weight: bold;',  align: 'center'},
                {field: 'register_date', title: '登记日期',width: 200, align: 'center', sort: true},
            ]]
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });



        //转给同事
        $('#assignment').click(function () {
            var type_choice =   $("#type_choice").val();
            let checkStatus;
            if(type_choice == 1){
                checkStatus = table.checkStatus('datalist_student'); //test即为基础参数id对应的值
            }else if(type_choice == 2){
                checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            }
            if (checkStatus.data.length == 0) {
                layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
                });
                return false;
            }
            let campus_id = checkStatus.data[0].campus_id;
            let lg = checkStatus.data.length;
            let checked_ids = [];
            for (let index = 0; index < checkStatus.data.length; index++) {
                const element = checkStatus.data[index];
                checked_ids.push(element.id)
            }
            let data = {"ids": checked_ids.join(',')};
            console.log(data);
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '转给同事 (共'+lg+'条)',
                area: ['60%', '60%'],
                shadeClose: false,
                btnAlign: 'c',
                shade: 0,
                content: "{:url('Student/assignment')}?ids=" + checked_ids + '&campus_id='+campus_id,
            });
        });

        /*报名-续签-扩科*/
        $('#again_up').click(function () {
            var type_choice =   $("#type_choice").val();
            let checkStatus;
            if(type_choice == 1){
                checkStatus = table.checkStatus('datalist_student'); //test即为基础参数id对应的值
            }else if(type_choice == 2){
                checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            }
            if (checkStatus.data.length == 0) {
                layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
                });
                return false;
            }
            let campus_id = checkStatus.data[0].campus_id;
            let checked_ids = [];
            for (let index = 0; index < checkStatus.data.length; index++) {
                const element = checkStatus.data[index];
                //该客户是系统或者无效则无法领取
                if (element.course_consultant_type == 2 || element.status == 2) {
                    layer.msg('所选数据中已存在公海数据或无效数据,请重新选择', {icon: 7, time: 2000}, function () {
                        tableIns_student.reload();
                    });
                    return false;
                }
                checked_ids.push(element.id)
            }
            if(checked_ids.length > 1){
                layer.msg('所选数据存在多条,请重新选择其中一条', {icon: 7, time: 2000}, function () {
                    tableIns_student.reload();
                });
                return false;
            }
            $.ajax({
                url: '{:url("Customer/delete_invalid_contract")}',
                method: "post",
                success: function (result) {
                    if (result.code === 1) {
                        location.href = '{:url("Customer/course_contract")}?student_id=' + checked_ids[0]+'&type='+1+'&campus_id='+campus_id+"&sourse_data=" + -1;
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });

        });

        /*学员排课*/
        $('#schedule_class').click(function () {
            var type_choice =   $("#type_choice").val();
            let checkStatus;
            if(type_choice == 1){
                checkStatus = table.checkStatus('datalist_student'); //test即为基础参数id对应的值
            }else if(type_choice == 2){
                checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            }
        })

        /*学员退班*/
        $('#retire').click(function () {

        });
        //订单分类选择
        $('.site-demo-active').on('click', function () {
            $('#typeclass').find('button').removeClass('layui-btn-warm');
            $(this).addClass('layui-btn-warm');
            let type = $(this).data('type');
            $("#type_data").val(type);  //数据类型

            if(type == 'no_all'){   //未排课
                document.getElementById("schedule_class").style.visibility='visible';
            }else{
                document.getElementById("schedule_class").style.visibility='hidden';
            }

            if(type == 'renewal_charge'){ //未缴费
                document.getElementById("again_up").style.visibility='hidden';
            }else{
                document.getElementById("again_up").style.visibility='visible';
            }



            if(type == 'all' || type == 'no_all'){

                document.getElementById("div_student").style.display="block";
                document.getElementById("div").style.display="none";
                $("#type_choice").val(1);  //选择全部
                tableIns_student.reload({
                    where: {
                        'type': type
                    }
                });


            }else{
                document.getElementById("div_student").style.display="none";
                document.getElementById("div").style.display="block";
                $("#type_choice").val(2); //选择非全部
                tableIns.reload({
                    where: {
                        'type': type
                    }
                });
            }


        });

        //监听工具条
        table.on('tool(Event)', function (obj) {
            let data = obj.data; //获得当前行数据
            let layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'sign_up') {
                //报名
               if(data.sign_up == 1 && data.arrears_status == 0){
                    if(data.contract_number_id === -1){
                        layer.msg('订单不存在,无法进行续费');
                        return;
                    }
                   console.log(data.contract_number_id)
                   console.log(data.contract_number_str)

                   layer.open({
                       type: 2,
                       skin: 'layui-layer-lan',
                       closeBtn: 2,
                       title: '选择补缴订单',
                       area: ['30%', '40%'],
                       shadeClose: false,
                       content: "{:url('student/choice_contract')}?contract_id=" + data.contract_number_id + '&contract_number_name=' +data.contract_number_str,
                       end:function () {
                           var contract_id = $("#contract_id").val();
                           var contract_name =     $("#contract_name").val();

                           if(contract_id == '' || contract_id.length <= 0 || contract_name == '' || contract_name <= 0){
                               return;
                           }
                           console.log(contract_id+contract_name);
                           let url = '{:url("Contract/receivables")}?id=' + contract_id;
                           layer.open({
                               type: 2,
                               skin: 'layui-layer-lan',
                               title: '订单收款：' + contract_name,
                               closeBtn: 2,
                               shadeClose: false,
                               area: ['90%', '95%'],
                               content: url,
                               cancel: function(index, layero){
                                   $.ajax({
                                       url: '/admin/contract/close',
                                       method: "post",
                                       beforeSend: function () {
                                           isLoad = layer.load(0, {
                                               shade: false
                                           }); //0代表加载的风格，支持0-2
                                       },
                                       success: function (result) {
                                           if (result.code === 1) {
                                               layer.close(index)
                                           }

                                       },
                                       complete: function () {
                                           layer.close(isLoad)
                                       },
                                       error: function (error) {
                                           console.log(error)
                                       }
                                   });
                                   return false;
                               },


                               end:function () {

                                   location.reload();
                               }

                           });
                       }
                   });


                }else{
                   $.ajax({
                       url: '/admin/customer/close',
                       method: "post",
                       beforeSend: function () {

                       },
                       success: function (result) {
                           if (result.code === 1) {

                               $.ajax({
                                   url: '{:url("Customer/delete_invalid_contract")}',
                                   method: "post",
                                   success: function (result) {
                                       if (result.code === 1) {

                                           location.href = '{:url("Customer/course_contract")}?student_id=' + data.id+'&type='+1+'&campus_id='+data.campus_id+"&sourse_data=" + -1;
                                       }
                                   },
                                   complete: function () {
                                       layer.close(isLoad)
                                   },
                                   error: function (error) {
                                       console.log(error)
                                   }
                               });
                           }

                       },
                       complete: function () {
                       },
                       error: function (error) {
                           console.log(error)
                       }
                   });

                }

            }
        });



        //搜索项
        $('#search').click(function () {
            let field = {};
            field.stype = $('select[name=stype] option:selected').val();
            field.type =    $("#type_data").val();
            field.follow_up = $('select[name=follow_up] option:selected').val();
            field.channel = $('select[name=channel] option:selected').val();
            field.user_id = $('select[name=user_id] option:selected').val();
            field.status_type = $('select[name=status_type] option:selected').val();
            field.keywords = $('input[name=keywords]').val();
            field.timetable = $('select[name=timetable] option:selected').val();
            field.grade_class = $('select[name=grade_class] option:selected').val();
            field.grade_headmaster = $('select[name=grade_headmaster] option:selected').val();
            field.select_campus = $('select[name=select_campus] option:selected').val();
            field.personal_center_status = $('select[name=personal_center_status] option:selected').val();
            var date_range = $('input[name=date_range]').val();
            if(date_range.length > 0){
                strs_date=date_range.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }



            var type_choice =   $("#type_choice").val();

            if(type_choice == 1){
                tableIns_student.reload({
                    where: field,
                    method: 'POST',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }else if(type_choice == 2){
                tableIns.reload({
                    where: field,
                    method: 'POST',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }



        });
        //清除
        $('#clear').click(function () {
            $('.layui-input').each(function (index, element) {
                $(element).val('');
            })
            var type_choice =   $("#type_choice").val();
            if(type_choice == 1){
                tableIns_student.reload({
                    where: {
                        'type': $("#type_data").val(),
                        'keywords': '',
                        'status_type':'',
                        'date_range':'',
                        'start_date':'',
                        'select_campus':'',
                        'end_date':'',
                        'grade_class':'',
                        'grade_headmaster':'',
                        'timetable':'',
                        'personal_center_status':'',
                        'follow_up': '',
                        'channel': '',
                        'user_id': '',

                    },
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }else if(type_choice == 2){
                tableIns.reload({
                    where: {
                        'type': $("#type_data").val(),
                        'keywords': '',
                        'status_type':'',
                        'date_range':'',
                        'start_date':'',
                        'timetable':'',
                        'select_campus':'',
                        'grade_class':'',
                        'grade_headmaster':'',
                        'end_date':'',
                        'follow_up': '',
                        'personal_center_status':'',
                        'channel': '',
                        'user_id': '',
                    },
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }

        });


        /***********导入学员************/
            //文件上传
        var isLoad;
        var uploadInst = upload.render({
            elem: '#btn_student',
            url: '/admin/Student/student_excel',
            before: function(obj) {
                isLoad = layer.load(0, {
                    shade: false
                });
            },
            done: function(res) {
                //如果上传失败
                if(res.code > 0) {
                    return layer.msg('导入失败');
                }
                //上传成功
                layer.msg(res.name+'导入成功');
                layer.close(isLoad);
                location.reload();
            },
            accept: 'file' ,//允许上传的文件类型
            //,size: 50 //最大允许上传的文件大小
            error: function() {
                return layer.msg('导入失败');
            }
        });

    });
</script>